package com.yami.shop.multishop.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.StockBillLog;
import com.yami.shop.bean.model.StockBillLogItem;
import com.yami.shop.bean.vo.StockBillLogItemImportVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.StockBillLogItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;


/**
 * 出入库商品项
 *
 * @author LGH
 * @date 2021-09-09 13:11:15
 */
@RestController
@RequestMapping("/shop/stockBillLogItem" )
@Tag(name = "出入库明细项接口")
public class StockBillLogItemController {

    @Autowired
    private  StockBillLogItemService stockBillLogItemService;
    @Value("${yami.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/list")
    @Operation(summary =  "通过出入明细id查询关联的商品列表")
    public ServerResponseEntity<List<StockBillLogItem>> list(@RequestParam("stockBillLogId") Long stockBillLogId) {
        List<StockBillLogItem> stockBillLogItemList = stockBillLogItemService.listByStockBillLogId(stockBillLogId);
        return ServerResponseEntity.success(stockBillLogItemList);
    }

    @GetMapping("/downloadModel")
    @Operation(summary =  "下载导入商品项模板", description = "下载导入商品项模板")
    public void downloadModel(HttpServletResponse response, Integer type) {
        stockBillLogItemService.downloadModel(response, type);
    }

    @PostMapping("/importExcel")
    @Operation(summary =  "批量导入商品项", description = "批量导入商品项")
    public ServerResponseEntity<StockBillLogItemImportVO> importExcel(@RequestParam("excelFile") MultipartFile excelFile, @RequestParam("type") Integer type) throws Exception {
        if (Objects.isNull(excelFile)) {
            throw new YamiShopBindException("yami.network.busy");
        }
        Long shopId = SecurityUtils.getShopUser().getShopId();
        StockBillLogItemImportVO stockBillLogItemImportVO = stockBillLogItemService.parseFile(excelFile, shopId, type);
        return ServerResponseEntity.success(stockBillLogItemImportVO);
    }

    @GetMapping("/flow")
    @Operation(summary =  "库存流水", description = "库存流水")
    public ServerResponseEntity<IPage<StockBillLogItem>> flow(PageParam<StockBillLog> page,@ParameterObject StockBillLog stockBillLog) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        stockBillLog.setShopId(shopId);
        IPage<StockBillLogItem> stockBillLogItemPage = stockBillLogItemService.flow(page, stockBillLog);
        for(StockBillLogItem stockBillLogItem : stockBillLogItemPage.getRecords()){
            if (BooleanUtil.isFalse(permission) && StrUtil.isNotBlank(stockBillLogItem.getEmployeeMobile())) {
                stockBillLogItem.setEmployeeMobile(PhoneUtil.hideBetween(stockBillLogItem.getEmployeeMobile()).toString());
            }
        }
        return ServerResponseEntity.success(stockBillLogItemPage);
    }

    @GetMapping("/exportFlow")
    @Operation(summary =  "导出库存流水", description = "导出库存流水")
    public void exportFlow(@ParameterObject StockBillLog stockBillLog, HttpServletResponse response) {
        Long shoId = SecurityUtils.getShopUser().getShopId();
        stockBillLog.setShopId(shoId);
        stockBillLogItemService.exportFlow(stockBillLog, response);
    }
}
